﻿using ConfExpert.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevTeam;
using ConfExpert.Datasets;
using ConfExpert.Utility;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;

namespace ConfExpert.Forms.Depo.Kauf
{
    public partial class ModelhaneBirimMetraj : XForm
    {
        string SatirListesi;
        int DagilimId = 0;
        public ModelhaneBirimMetraj(string SatirListesi)
        {
            this.SatirListesi = SatirListesi;

            InitializeComponent();
        }

        private void ModelhaneBirimMetraj_Load(object sender, EventArgs e)
        {
            this.Text = "Modelhaneye İletilecek Bilgiler Ekranı";
            gridView1.Init("GetModelhaneBirimMetraj", SatirListesi);
            gridView1.AddDetailGrid(gridView2, "BMIstekId");
            gridView1.MakeColumnInvisible("BMIstekId");
            gridView2.MakeColumnInvisible("BMIstekLinesId");
            gridView1.MakeColumnsReadOnly("Model", "Kalip", "Line", "DagilimId", "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28");
            gridView2.MakeColumnsReadOnly("Nr", "ProductId", "StokKodu", "BM", "YikamaSonucu");
            //gridView1.YanYanaGetir("DagilimId:KumasEni");

            for (int i = 1; i <= 28;i++)
            {
                DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[string.Format("A{0:00}", i)];
                col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                col.DisplayFormat.FormatString = "P1";
            }

            gridView1.MakeRowSelectable();
            DagilimChanged();

            MakeDagilimColumnsInvisible(gridView1);

            gridView1.AddSummary();
               
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
        }


        public void MakeDagilimColumnsInvisible(GridView gv)
        {
            bool ValueDetected = false, BreakSuccess = false;
            //string cellValue0, cellValue1, cellValue2, cellValue3, cellValue6;
            decimal cellValue;
            string ColumnHeader;
            int GridRowCount = gv.DataRowCount;
            for (int i = 28; i > 0; --i)
            {
                ColumnHeader = String.Format("A{0:00}", i);
                for (int j = 0; j < GridRowCount; ++j)
                {

                    cellValue = gv.GetRowCellValue(gv.GetRowHandle(j), ColumnHeader).ToDecimal();

                    if (cellValue > 0)
                    {
                        ValueDetected = true;
                        
                    }

                    if (ValueDetected)
                    {
                        BreakSuccess = true;
                        break;
                    }
                }
                if (BreakSuccess)
                    break;
                else
                    gv.MakeColumnInvisible(ColumnHeader);
            }

            // reset variables
            ValueDetected = BreakSuccess = false;

            for (int i = 0; i < 29; ++i)
            {
                ColumnHeader = String.Format("A{0:00}", i);
                for (int j = 0; j < GridRowCount; ++j)
                {

                    cellValue = gv.GetRowCellValue(gv.GetRowHandle(j), ColumnHeader).ToDecimal();

                    if (cellValue > 0)
                    {
                        ValueDetected = true;

                    }

                    if (ValueDetected)
                    {
                        BreakSuccess = true;
                        break;
                    }
                }
                if (BreakSuccess)
                    break;
                else
                    gv.MakeColumnInvisible(ColumnHeader);
            }
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DagilimChanged();
        }

        private void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView2.DataTable().Select("GarniPos is null OR KumasEni is null OR KumasRaporu is null OR ISNULL(DesenYonuId, 0) = 0").GetLength(0) > 0)
            {
                XtraMessageBox.Show("Boş Alan Bıraktınız veya Geçersiz Karakter Girişi Yaptınız, Lütfen Tekrar Kontrol Ediniz!", "Uyarı");
                //table = GridiDataTableYap(mbm.gridView1);
                //DialogTekrarGoster = true;

            }
            else
            {
                ExecuteNonQuery("UpdModelhaneBirimMetraj", gridView1.DataTable().Serialize(), gridView2.DataTable().Serialize());//mbm.gridView1.Serialize(), mbm.gridView2.Serialize());
                //if (DialogTekrarGoster)
                //    DialogTekrarGoster = false;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            
        }

        private void DagilimChanged()
        {
            try
            {
                gridView1.CloseEditor();
                gridView1.BeginUpdate();
                if (DagilimId != gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DagilimId").ToInt(-1))
                {
                    DagilimId = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DagilimId").ToInt(-1);

                    MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(DagilimId);
                    if (r != null)
                        for (int i = 1; i < 29; i++)
                        {
                            string fieldName = String.Format("A{0:00}", i);
                            string cap = r[String.Format("A{0:00}", i)].ToString();
                            gridView1.Columns[fieldName].Caption = cap != string.Empty ? cap : " ";
                        }
                }

            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                gridView1.EndUpdate();
            }
        }

    }
}
